package com.xiaomage.sharedpreferences_contentprovider

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        SPHelper.init(application)
        bt_save_int.setOnClickListener{
            val start = System.currentTimeMillis()
            SPHelper.save("a", 100)
            SPHelper.save("z", true)
            val end = System.currentTimeMillis()
            Toast.makeText(this, "takes " + (end - start) + "millis", Toast.LENGTH_SHORT).show()
        }
        bt_save_string.setOnClickListener{
            val start = System.currentTimeMillis()
            SPHelper.save("b", "aaa")
            SPHelper.save("x", 1.001f)
            val end = System.currentTimeMillis()
            Toast.makeText(this, "takes " + (end - start) + "millis", Toast.LENGTH_SHORT).show()
        }
        bt_save_string_set.setOnClickListener{
            val set = HashSet<String>()
            set.add("aaa,bbb")
            set.add("ccc")
            val start = System.currentTimeMillis()
            SPHelper.save("c", set)
            val end = System.currentTimeMillis()
            Toast.makeText(this, "takes " + (end - start) + "millis",
                    Toast.LENGTH_SHORT).show()
        }
        bt_clean.setOnClickListener {
            val start = System.currentTimeMillis()
            SPHelper.clear()
            val end = System.currentTimeMillis()
            Toast.makeText(this, "takes "
                    + (end - start) + "millis", Toast.LENGTH_SHORT).show()
        }
        bt_get_value.setOnClickListener {
            val start = System.currentTimeMillis()

            val map = SPHelper.getAll()

            //        String result1 = "a="+SPHelper.getInt("a",0);
            //        String result2 = "b="+SPHelper.getString("b","");
            //
            //        Set<String> set = SPHelper.getStringSet("c",null);

            val end = System.currentTimeMillis()
            Toast.makeText(this, "takes " + (end - start) + "millis", Toast.LENGTH_SHORT).show()
            var result = ""
            for (entry in map.entries) {
                val k = entry.key
                var v: Any? = entry.value
                if (v is Set<*>) {
                    var result3 = ""
                    for (string in v as Set<String>) {
                        result3 += "\"" + string + "\""
                        result3 += "    "
                    }
                    v = result3
                }
                result += k + "=" + v + "\n"
            }
            values.text = result
        }
    }

}
